package com.dengbw.systemtransfer.service;

import com.dengbw.systemtransfer.global.Column;
import com.dengbw.systemtransfer.mapper.SysMenuMapper;
import com.dengbw.systemtransfer.mapper.SysRoleMapper;
import com.dengbw.systemtransfer.mapper.SysRoleMenuMapper;
import com.dengbw.systemtransfer.model.db.SysRole;
import com.dengbw.systemtransfer.model.db.SysRoleMenu;
import com.dengbw.systemtransfer.model.function.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author dengbw
 * @date 2021/11/11 4:17 下午
 */
@Service
public class MenuService {

    SysMenuMapper sysMenuMapper;
    SysRoleMapper sysRoleMapper;
    SysRoleMenuMapper sysRoleMenuMapper;
    SysRoleService sysRoleService;

    @Autowired
    public MenuService(SysMenuMapper sysMenuMapper, SysRoleMapper sysRoleMapper, SysRoleMenuMapper sysRoleMenuMapper, SysRoleService sysRoleService) {
        this.sysMenuMapper = sysMenuMapper;
        this.sysRoleMapper = sysRoleMapper;
        this.sysRoleMenuMapper = sysRoleMenuMapper;
        this.sysRoleService = sysRoleService;
    }

    @Transactional(rollbackFor = Exception.class)
    public void saveMenu(String zhenCorpId, Long cunSubId) {
        Task.menuTask(zhenCorpId, cunSubId).stream()
                .parallel()
                .forEach(m -> sysMenuMapper.insert(m));
        sysRoleService.saveRole(zhenCorpId, cunSubId);
        Task.roleMenuTask(zhenCorpId, cunSubId)
                .forEach(r -> sysRoleMenuMapper.insert(r));
    }
}
